﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter21.Models.ParallelSamples
{
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Threading;

    public static class Demo
    {
        public static void InvokeAll()
        {
            Action line = () =>
                {
                    Console.ReadLine();
                };
            Invoke(For, line, ForAwait, line, ForBreak, line, ForInitial, line, ForEach, line, Invoke, line);
        }
        public static void Invoke(params Action[] actions)
        {
            foreach (var action in actions)
            {
                action();
            }
        }
        public static void For()
        {
            var result = Parallel.For(0, 10, i =>
            {
                Console.WriteLine("循环: {0:00}, 任务: {1:00}, 线程: {2:00}", i, Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
                Thread.Sleep(16);
            });
            Console.WriteLine("是否完成：{0}", result.IsCompleted);
        }
        public static void ForAwait()
        {
            var result = Parallel.For(0, 10, async i =>
            {
                Console.WriteLine("循环: {0:00}, 任务: {1:00}, 线程: {2:00}", i, Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
                await Task.Delay(16);
            });
            Console.WriteLine("是否完成：{0}", result.IsCompleted);
        }
        public static void ForBreak()
        {
            var result = Parallel.For(0, 40, async (i, pls) =>
            {
                Console.WriteLine("循环: {0:00}, 任务: {1:00}, 线程: {2:00}", i, Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
                await Task.Delay(16);
                if (i > 15)
                {
                    pls.Break();
                }
            });
            Console.WriteLine("是否完成：{0}", result.IsCompleted);
            Console.WriteLine("最低中断迭代: {0}", result.LowestBreakIteration);
        }
        public static void ForInitial()
        {
            var result = Parallel.For<string>(0, 20, () =>
            {
                Console.WriteLine("初始化线程 {0:00}, 任务 {1:00}", Thread.CurrentThread.ManagedThreadId, Task.CurrentId);
                return string.Format("线程 {0:00}", Thread.CurrentThread.ManagedThreadId);
            }, (i, pls, str1) =>
            {
                Console.WriteLine("代码体: {0:00} 字符 {1} 线程 {2:00} 任务 {3:00} ", i, str1, Thread.CurrentThread.ManagedThreadId, Task.CurrentId);
                Thread.Sleep(16);
                return string.Format("i {0:00}", i);
            }, (str1) =>
            {
                Console.WriteLine("最终: {0}", str1);
            });
        }
        public static void ForEach()
        {
            string[] data = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve" };
            var result = Parallel.ForEach<string>(data, s =>
            {
                Console.WriteLine(s);
            });
        }
        public static void Invoke()
        {
            Console.WriteLine("");
            Parallel.Invoke(For, ForAwait, ForBreak, ForInitial, ForEach);
        }
    }
}
